package test;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class ThemesWindow extends JFrame {
	JComboBox comboBoxThemesNames;
	JButton buttonOk,buttonApply;
	ChatterWindows chatterWindows;
	Font fontTemp;
	LookAndFeelInfo[] arrLookAndFeel;
	List<String> listlookAndFieldName;

	public ThemesWindow(ChatterWindows chatterWindows) throws HeadlessException {
		super("Choose Themes...");
		this.chatterWindows = chatterWindows;
		this.getContentPane().setLayout(null);
		this.initThemes();


		this.buttonOk = new JButton("Ok");
		this.buttonApply = new JButton("Apply");
		String[] themesNames = this.getArrStringNameLookAndFeel();


		this.comboBoxThemesNames = new JComboBox(themesNames);


		buttonApply.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				applyAction();

			}
		});

		buttonOk.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				applyAction();
				ThemesWindow.this.setVisible(false);
			}
		});

		this.comboBoxThemesNames.setBounds(5,5,200,25);
		this.buttonOk.setBounds(220, 5, 50, 23);
		this.buttonApply.setBounds(280, 5, 70, 25);

		this.getContentPane().add(comboBoxThemesNames);
		this.getContentPane().add(buttonOk);
		this.getContentPane().add(buttonApply);

		this.setSize(380,80);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);


	}

	public static void main(String[] args) {
		try {
			Locale.setDefault(new Locale("en"));
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		new FontWindow(null);
	}
	public void applyAction(){

			String selectedName = (String) this.comboBoxThemesNames.getSelectedItem();

			try {
			    for (LookAndFeelInfo info : this.arrLookAndFeel) {
			        if (selectedName.equals(info.getName())) {
			            UIManager.setLookAndFeel(info.getClassName());
			            this.chatterWindows.refeshThemes();
			            break;
			        }
			    }
			} catch (Exception e) {
			    JOptionPane.showMessageDialog(this, "Error:" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}

	}

	private void initThemes() {

		try {
			this.listlookAndFieldName = new LinkedList<String>();
			this.arrLookAndFeel = UIManager.getInstalledLookAndFeels();
			for (LookAndFeelInfo lookAndFeel : this.arrLookAndFeel) {
				listlookAndFieldName.add(lookAndFeel.getName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String[] getArrStringNameLookAndFeel() {
		if (this.listlookAndFieldName == null || this.listlookAndFieldName.size() == 0) {
			return new String[0];
		} else {
			String[] reValues = new String[this.listlookAndFieldName.size()];
			for (int i = 0; i < reValues.length; i++) {
				reValues[i] = this.listlookAndFieldName.get(i);
			}
			return reValues;
		}

	}

}
